package com.citcc.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.citcc.api.utils.GpsAroundUtil;
import com.citcc.api.utils.MapDistanceUtil;
import com.citcc.business.bean.PositionModel;
import com.citcc.business.bean.vo.CdzStationVo;
import com.citcc.business.service.CdzStationService;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.utils.StringUtils;
import com.citcc.framework.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

@Api(tags = "充电站api")
@RestController
@RequestMapping("/station")
public class CdzStationController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(CdzStationController.class);
    private final CdzStationService cdzStationService;

    public CdzStationController(CdzStationService cdzStationService) {
        this.cdzStationService = cdzStationService;
    }

    /**
     * DecimalFormat 内部静态方法较多，设置单例
     */
    private static class DecimalFormatProvider {
        private static final DecimalFormat format = new DecimalFormat("#,##0.00");
    }

    public static DecimalFormat getFormat() {
        return CdzStationController.DecimalFormatProvider.format;
    }

    @ApiOperation("获取定位5千米以内充电站及空闲充电桩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon", value = "经度,最多保留6位", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "纬度,最多保留6位", paramType = "query")
    })
    @GetMapping("/v2/list")
    public AjaxResult getStationListByRadii(@RequestParam double lon, @RequestParam double lat) {
        PositionModel aroundLocation = GpsAroundUtil.getAroundLocation(lon, lat, 5);
        List<CdzStationVo> stationList = cdzStationService.getStationListByRadii(aroundLocation);
        return success(200, stationList);
    }

    @ApiOperation("查询指定ID充电站详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon", value = "经度,最多保留6位", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "纬度,最多保留6位", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "充电站ID", paramType = "query")
    })
    @GetMapping("/v2/get")
    public AjaxResult getStationById(@RequestParam String lon, @RequestParam String lat, @RequestParam String stationId) {
        logger.info("lon  "+lon);
        logger.info("lat  "+lat);
        if (StringUtils.isEmpty(lon)||StringUtils.isEmpty(lat)){
            return error(-1,"坐标异常");
        }
        logger.info("stationId "+stationId);
        CdzStationVo station = cdzStationService.getStationById(stationId);
        logger.info("station  "+station);
        if (station==null){
            return error(-1,"充电站不存在");
        }
        String json = MapDistanceUtil.mapDistanceMethod(new String[]{lon + "," + lat},
                station.getLon() + "," + station.getLat(), "0");
        JSONObject jsonObject = JSON.parseObject(json);
        if (jsonObject.get("status").equals("1")) {
            JSONArray resultList = jsonObject.getJSONArray("results");
            JSONObject object = resultList.getJSONObject(0);
            String distance = object.getString("distance");
            BigDecimal bigDecimal = new BigDecimal(distance);
            if (bigDecimal.doubleValue()>=1000){
                BigDecimal bd = new BigDecimal(1000);
                double value = bigDecimal.divide(bd).doubleValue();
                distance = getFormat().format(value) + "千米";
            }else{
                distance = distance + "米";
            }
            station.setDistance(distance);
        }
        return success(200, station);
    }

}
